(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.ppfCommunity.createVerifyController', createVerifyController);
	createVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'ppfCommunityService', '$filter' ];
	function createVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, ppfCommunityService, $filter) {
		var ctrl = this, ppfCommunityInfo = $scope.$stateParams.params, errorMessage = {};
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.ppfCommunity = {};
		
		ctrl.init = function() {
			var params = {
					taskId : ppfCommunityInfo.taskId
			};
			// init acct
			ppfCommunityService.initProfAcct(layoutConfig, $scope, ctrl);
			// init int
			//ppfCommunityService.initProfInt(layoutConfig, $scope, ctrl);
			//ppfCommunityService.initProfCostOfFundInt(layoutConfig, $scope, ctrl);
			// init pastdue
			ppfCommunityService.initProfPastdue(layoutConfig, $scope, ctrl);
			// init fee
			ppfCommunityService.initProfFee(layoutConfig, $scope, ctrl);
			// init doc
			ppfCommunityService.initEventDoccfg(layoutConfig, $scope, ctrl);
			// init limit facility
			//ppfCommunityService.initProfLimitFaci(layoutConfig, $scope, ctrl);
			//ppfCommunityService.initProfLimitInsFaci(layoutConfig, $scope, ctrl);
			//ppfCommunityService.initProfLimitPurFaci(layoutConfig, $scope, ctrl);
			// init Settlement Instruction
			ppfCommunityService.initProfSetlinst(layoutConfig, $scope, ctrl);
			// init ppfmember
			ppfCommunityService.initPpfMember(layoutConfig, $scope, ctrl);
			// init cust prop define
			ppfCommunityService.initCustProp(layoutConfig, $scope, ctrl);
			//
			ppfCommunityService.initGeneral($scope, ctrl, true).then(function() {
				authService.createVerify(params).then(function(response) {
					var initPpfCommunity = response.data.model;
					ctrl.action = "createVerify";
					ppfCommunityService.initLayoutConfigAndModel($scope, ctrl, initPpfCommunity, ctrl.action, false).then(function() {
						// ctrl.ppfCommunity = initPpfCommunity; done in initLayoutConfig
						ctrl.ppfCommunity.taskId = response.data.task.taskId || ppfCommunityInfo.taskId;
					});
				});
			});
		};

		ctrl.createVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [$filter("encode")(ctrl.ppfCommunity.buyerCode)]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createVerifyApprove(ctrl.ppfCommunity).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.createVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.ppfCommunity.comments = comments;
						authService.createVerifyReject(ctrl.ppfCommunity).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};
		
		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.ppfcommunity.list',
					'state' : 'cda.ppfCommunity.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.ppfcommunity.list',
					'state' : 'cda.ppfCommunity.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);
